package com.feifan.monthhelper.calculate;

import com.feifan.monthhelper.utils.DateUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * 抽象算法
 * @param <T>
 */
public abstract class AbstractMenstrualCalculate<T> {

//    public static final int MENSTRUAL_DAY = 1;
//    public static final int CACULATE_MENSTRUAL_DAY = 2;
//    public static final int SAFE_DAY = 3;
//    public static final int OVULATORY_DAY = 4;

    private Date date;
    private int menstrual_period;
    private int span_period;

    public AbstractMenstrualCalculate() {
    }

    public AbstractMenstrualCalculate(Date date, int menstrual_period, int span_period) {
        this.date = date;
        this.menstrual_period = menstrual_period;
        this.span_period = span_period;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDate() {
        return date;
    }

    public void setMenstrual_period(int menstrual_period) {
        this.menstrual_period = menstrual_period;
    }

    public void setSpan_period(int span_period) {
        this.span_period = span_period;
    }

    public int getMenstrual_period() {
        return menstrual_period;
    }

    public int getSpan_period() {
        return span_period;
    }

    public int
    getDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        System.out.println(" day of Month = " + calendar.get(Calendar.MONTH));
                //setTime之后先get一次，让calendar重新计算一次时间，否则不会计算
        calendar.get(Calendar.DAY_OF_YEAR);
        System.out.println(" day of Year = " + DateUtils.init().dateToString(calendar.getTime(), DateUtils.DateStyle.YYYY_MM_DD));
        return calendar.get(Calendar.DAY_OF_YEAR);
    }

    AbstractMenstrualCalculate<T> nextCalculate;

    public final T process(T request) {
        if(canCalculate(request)) {
            return calculate(request);
        } else if (nextCalculate != null){
            return nextCalculate.process(request);
        }
        return request;
    }

    abstract boolean canCalculate(T request);
    abstract T calculate(T request);

    public void setNextCalculate(AbstractMenstrualCalculate menstrualCalculate) {
        this.nextCalculate = menstrualCalculate;
    }
}
